﻿using System.Configuration;
using iLib.Cryptography;

namespace Cms.Utilities
{
    public class Cipher
    {
        public static string Encrypt(string cleartextCode)
        {
            var re = new RijndaelEnhanced(
                ConfigurationManager.AppSettings["CipherPassphrase"],
                ConfigurationManager.AppSettings["CipherInitVector"],
                8,
                8,
                128,
                null,
                ConfigurationManager.AppSettings["CipherSalt"]);
            return re.Encrypt(cleartextCode);
        }

        public static string Decrypt(string encryptedCode)
        {
            var re = new RijndaelEnhanced(
                ConfigurationManager.AppSettings["CipherPassphrase"],
                ConfigurationManager.AppSettings["CipherInitVector"],
                8,
                8,
                128,
                null,
                ConfigurationManager.AppSettings["CipherSalt"]);
            try
            {
                return re.Decrypt(encryptedCode);
            }
            catch
            {
                return null;
            }
        }
    }
}